package cn.kgc.springcloud.controller;

import cn.hutool.core.util.IdUtil;
import cn.kgc.springcloud.entity.Clazz;
import cn.kgc.springcloud.service.ClazzService;
import cn.kgc.springcloud.vo.ClazzVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author: 课工场
 * @since 2023/9/12
 */
@RestController
@RequestMapping("clazz")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;
    //查询班级列表
    @GetMapping("list")
    public List<ClazzVO>  clazzVOList(){

         return  clazzService.getList();

    }

    // 根据id 删除班级信息
    @DeleteMapping("deleteById/{id}")
    public Boolean deleteById(@PathVariable("id") Integer id){
        return  clazzService.removeById(id);
    }

    //添加班级信息
    @PostMapping("add")
    public Clazz addClazz(MultipartFile file,String name,Integer tagId){
        String lastName = null;

        if (!file.isEmpty()){

            String originalFilename = file.getOriginalFilename();
            String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
            // uuid
            String uuid = IdUtil.simpleUUID();
            lastName = uuid+"."+filenameExtension;
            File f = new File("D:\\upload\\" + lastName);
            try {
                file.transferTo(f);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Clazz clazz = new Clazz();
        clazz.setPath(lastName);
        clazz.setTagid(tagId);
        clazz.setName(name);
        clazzService.save(clazz);
        return clazz;

    }


    // 根据id 获取班级信息
    @GetMapping("getById/{id}")
    public Clazz  getById(@PathVariable("id") Integer id){
        return  clazzService.getById(id);
    }

}
